﻿var Cookie = {
    create: function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";

        var domain = window.location.hostname;
        
        if (Cookie.hasSubDomain(domain)) {
            var separate = domain.split('.');
            separate.shift();
            domain = separate.join('.');
        }
        
        document.cookie = name + "=" + value + expires + "; path=/; domain=." + domain;
    },

    read: function (name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    },

    // Source: http://snipplr.com/view/5449/check-if-a-url-contains-a-subdomain/
    hasSubDomain: function (url) {
        url = url.replace(new RegExp(/^\s+/), ""); // START
        url = url.replace(new RegExp(/\s+$/), ""); // END
        // If found, convert back slashes to forward slashes
        url = url.replace(new RegExp(/\\/g), "/");

        // If there, removes 'http://', 'https://' or 'ftp://' from the start
        url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i), "");

        // If there, removes 'www.' from the start of the string
        url = url.replace(new RegExp(/^www\./i), "");

        // remove complete string from first forward slash on
        url = url.replace(new RegExp(/\/(.*)/), "");

        // Removes '.??.??' or '.???.??' from end - e.g. '.co.uk', '.com.au'
        if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
            url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i), "");
            // Removes '.??' or '.???' or '.????' from end - e.g. '.us', '.com', '.info'
        } else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
            url = url.replace(new RegExp(/\.[a-z]{2,4}$/i), "");
        }

        // Check to see if there is a dot '.' left in the string
        var subDomain = (url.match(new RegExp(/\./g))) ? true : false;

        return (subDomain);
    }
};

(function ($) {
window.MobilePack = {
    init: function () {
        MobilePack.deviceResolution();

        $(document).ready(function() {
            $('.mobilePage').live('swipeleft swiperight', function (event) {
                if (event.type == "swipeleft") {
                    MobilePack.previousPage();
                }
                
                if (event.type == "swiperight") {
                    MobilePack.nextPage();
                }
                
                event.preventDefault();
            });
            
            $('.toggle-desktop-mobile').live('click', function (e) {
                e.preventDefault();

                MobilePack.checkToggleModeCookie();

                location.href = this.href;
            });
        });
    },
    
    previousPage: function () {
        var prev = $(".prevlink", $.mobile.activePage);
        if (prev.length) {
            var prevurl = $(prev).attr("href");
            $.mobile.changePage(prevurl, "slideright");
        }
    },
    
    nextPage: function () {
        var next = $(".nextlink", $.mobile.activePage);
        if (next.length) {
            var nexturl = $(next).attr("href");
            $.mobile.changePage(nexturl, "slideleft");
        }
    },
    
    pageInit: function (event) {
        // Look for external links and mark them external (jQuery Mobile won't use Ajax to load these links)
        $('.bodyContent a:not(.mobileLink)', this).attr('rel', 'external');

        MobilePack.clientSideImageResizing(this);
    },
    
    checkToggleModeCookie: function () {
        var mobileToggleValue = Cookie.read('mobileToggle');

        if (mobileToggleValue == undefined || mobileToggleValue === 'mobile') {
            mobileToggleValue = 'normal';
        }
        else {
            mobileToggleValue = 'mobile';
        }

        Cookie.create('mobileToggle', mobileToggleValue, 365);
    },
    deviceResolution: function () {
        /* 
        Source: http://adaptive-images.com/
        we want to get the image for the largest window size the device can support, not just the current window size */
        var device_width = window.innerWidth; // get screen width
        var device_height = window.innerHeight; // get the screen height

        /* if this is a device that can rotate, we need the longest edge, not just the current width */
        if (device_width > device_height) {
            ri_width = device_width;
        } else {
            ri_width = device_height;
        }

        /* set the client width in a cookie for the server to read */
        Cookie.create('resolution', ri_width, 365);
    },
    
    clientSideImageResizing: function (context) {
        $('.bodyContent img[width]', context).each(function (i) {
            if (parseInt($(this).attr('width')) > window.innerWidth) {
                var w = parseInt($(this).attr('width'));
                var h = parseInt($(this).attr('height'));
                var mw = window.innerWidth-15;
                var nh =  ((mw*h) / w);
                $(this).attr('width', mw);
                $(this).attr('height', nh);
            }
        });
    }
};

MobilePack.init();
} (jQuery));

// Source: http://html5boilerplate.com/mobile
(function(document){
    window.MBP = window.MBP || {};
 
    // Fix for iPhone viewport scale bug
    // http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
    MBP.viewportmeta = document.querySelector && document.querySelector('meta[name="viewport"]');
    MBP.ua = navigator.userAgent;
 
    MBP.scaleFix = function () {
      if (MBP.viewportmeta && /iPhone|iPad/.test(MBP.ua) && !/Opera Mini/.test(MBP.ua)) {
        MBP.viewportmeta.content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
        document.addEventListener("gesturestart", MBP.gestureStart, false);
      }
    };
    
    MBP.gestureStart = function () {
        MBP.viewportmeta.content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6";
    };
})(document);